package com.starsoft.smdc.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.starsoft.frame.core.orm.BaseDao;
import com.starsoft.frame.core.orm.PageObject;
import com.starsoft.frame.util.StringUtil;
import com.starsoft.smdc.bean.SmdcSecurity;
import com.starsoft.smdc.util.SmdcConst;

@Repository
public class SmdcSecurityDao extends BaseDao<SmdcSecurity, String>{

	public List<SmdcSecurity> getByTicker(String ticker){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ticker", ticker);
		return findByMap(params);
	}

	public List<SmdcSecurity> get(List<String> secIds) {
		List<SmdcSecurity> result = new ArrayList<SmdcSecurity>();
		for(String id : secIds){
			SmdcSecurity security =  get(id);
			if(security!=null){
				result.add(security);
			}
		}
		return result;
	}
	
	public Date getBeginDate(SmdcSecurity security) {
		Date beginDate = SmdcConst.SMDC_BEGIN_DATE;
		if (security.getListDate()!=null && beginDate.before(security.getListDate())) {
			beginDate = security.getListDate();
		}
		return beginDate;
	}
	
	public Map<String, Object> pageQuery(PageObject po, String secId, String exchange, String secType, String fundCategory) {
		List<Criterion> criterions = new ArrayList<Criterion>();
		if(!StringUtil.isEmpty(secId)){
			Criterion a = Restrictions.like("secId", secId.trim(), MatchMode.ANYWHERE);
			Criterion b = Restrictions.like("secName", secId.trim(), MatchMode.ANYWHERE);
			criterions.add(Restrictions.or(a, b));
		}
		if(!StringUtil.isEmpty(exchange)){
			criterions.add(Restrictions.eq("exchange", exchange.trim()));
		}
		if(!StringUtil.isEmpty(secType)){
			criterions.add(Restrictions.eq("secType", secType.trim()));
		}
		if(!StringUtil.isEmpty(fundCategory)){
			criterions.add(Restrictions.eq("fundCategory", fundCategory.trim()));
		}
		return pageQuery(entityClass, po, criterions);
	}
	
	
	public Map<String, Object> pageQuery(PageObject po, String key) {
		List<Criterion> criterions = new ArrayList<Criterion>();
		if(!StringUtil.isEmpty(key)){
			Criterion like = Restrictions.or(Restrictions.like("secId", key, MatchMode.ANYWHERE),
					Restrictions.like("secName", key, MatchMode.ANYWHERE));
			criterions.add(like);
		}
		return pageQuery(entityClass, po, criterions);
	}
	
	
}
